//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet
{
    internal class DifferentialStyleRecord: IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("dxf"))
            {
                c.PassAttributes();
                if (c.ReaderLocalName == "font")
                {
                    Font = new Font();
                    Font.ReadXml(c);
                }
                else
                    Font = null;
                numFmt = context.ReadOptionalElement<InternalNumberFormat>("numFmt");
                Background = c.ReaderLocalName == "fill" ? Fill.ReadFill(c) : null;
                Alignment = c.ReadOptionalElement<CellAlignment>("alignment");
                Borders = c.ReadOptionalElement<Borders>("border");
                Protection = c.ReadOptionalElement<CellProtection>("protection");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("dxf"))
            {
                c.WriteOptionalElement(Font);
                c.WriteOptionalElement(NumberFormat);
                c.WriteOptionalElement(Background);
                c.WriteOptionalElement(Alignment);
                c.WriteOptionalElement(Borders);
                c.WriteOptionalElement(Protection);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        private InternalNumberFormat _numFmt;

        internal InternalNumberFormat NumberFormat
        {
            get { return _numFmt; }
            set { _numFmt = value; }
        }
	

        private ExtensionElement _extension;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; }
        }
	

        private Font font;

        internal Font Font
        {
            get { return font; }
            set { font = value; }
        }
        internal Font EnsureFont()
        {
            Contract.Ensures(Contract.Result<Font>() != null);
            if (Font == null)
                Font = new Font();
            return Font;
        }
        private InternalNumberFormat numFmt;

        internal InternalNumberFormat Format
        {
            get { return numFmt; }
            set { numFmt = value; }
        }
        private Fill fill;

        public Fill Background
        {
            get { return fill; }
            set { fill = value; }
        }
        public Fill Fill
        {
            get { return fill; }
            set { fill = value; }
        }private CellAlignment alignment;

        internal CellAlignment Alignment
        {
            get { return alignment; }
            set { alignment = value; }
        }
        private Borders border;

        public Borders Borders
        {
            get { return border; }
            set { border = value; }
        }
        private CellProtection protection;

        internal CellProtection Protection
        {
            get { return protection; }
            set { protection = value; }
        }


        public DifferentialStyleRecord Clone()
        {
            return this;
        }
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (DifferentialStyleRecord)) return false;
            return Equals((DifferentialStyleRecord) obj);
        }

        public bool Equals(DifferentialStyleRecord obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj._numFmt, _numFmt) && Equals(obj._extension, _extension) && Equals(obj.font, font) && Equals(obj.numFmt, numFmt) && Equals(obj.fill, fill) && Equals(obj.alignment, alignment) && Equals(obj.border, border) && Equals(obj.protection, protection);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (_numFmt != null ? _numFmt.GetHashCode() : 0);
                result = (result*397) ^ (_extension != null ? _extension.GetHashCode() : 0);
                result = (result*397) ^ (font != null ? font.GetHashCode() : 0);
                result = (result*397) ^ (numFmt != null ? numFmt.GetHashCode() : 0);
                result = (result*397) ^ (fill != null ? fill.GetHashCode() : 0);
                result = (result*397) ^ (alignment != null ? alignment.GetHashCode() : 0);
                result = (result*397) ^ (border != null ? border.GetHashCode() : 0);
                result = (result*397) ^ (protection != null ? protection.GetHashCode() : 0);
                return result;
            }
        }

        internal CellAlignment EnsureAlignment()
        {
            if (this.alignment == null)
                this.Alignment = new CellAlignment();
            return this.Alignment;
        }

        public CellProtection EnsureProtection()
        {
            if (protection == null)
                Protection = new CellProtection();
            return this.Protection;
        }
    }
}
